package ac.neec.timewatch;

import ac.neec.mio.resting.RestingMeasurementManager;

/**
 * Created by G011C1093 on 14/01/18.
 */
public class MeasurementTimer extends TimeWatchSubject {

	private long time;
	private boolean flag;

	public MeasurementTimer(long time) {
		this.time = time;
	}

	private long getCurrentTime() {
		return System.currentTimeMillis();
	}

	public void cancel() {
		flag = false;
	}

	public void startTimer() {
		final long startTime = getCurrentTime();
		flag = true;
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				while (flag) {
					time = (getCurrentTime() - startTime);
					if (time >= RestingMeasurementManager.RESTING_MEASUREMENT_TIME) {
						break;
					}
					notifyTimerUpdata(RestingMeasurementManager.RESTING_MEASUREMENT_TIME - time);
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				if (flag) {

					notifyAlarmObservers();
				}
				else {
					notifyCancelObservers();
				}
			}
		});
		thread.start();
	}

}
